package Util;

/*
 * ParseXMLFile.java
 *
 * Created on 4. listopad 2003, 10:14
 * by
 * http://labe.felk.cvut.cz/~xfaigl/mep/xml/java-xml.htm
 */

import Bloques.ParkedObject;
import Bloques.Segment;
import event.Event;
import java.awt.Graphics;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/** This class represents short example how to parse XML file,
 * get XML nodes values and its values.<br><br>
 * It implements method to save XML document to XML file too
 * @author Martin Glogar
 */
public class ParseXMLFile {
    
    private final static String xmlFileName = "c:/temp/MiNetwork.xml";
  //  private final static String targetFileName = "c:/Users/emmanuel/Documents/example2.xml";
    private int index = -1;
    private Segment segments[];
    private Event rootControl;
    private Graphics g;
    /** Creates a new instance of ParseXMLFile */
    public ParseXMLFile(Segment segments[],Event rootControl) {
        this.segments = segments;
        this.rootControl = rootControl;
        // parse XML file -> XML document will be build
        Document doc = parseFile(xmlFileName);
        // get root node of xml tree structure
        Node root = doc.getDocumentElement();
        // write node and its child nodes into System.out
        System.out.println("Statemend of XML document...");
        getInfo(root);
        System.out.println("... end of statement");
        // write Document into XML file
       // saveXMLDocument(targetFileName, doc);
    }
    
    /** Returns element value
     * @param elem element (it is XML tag)
     * @return Element value otherwise empty String
     */
    public final static String getElementValue( Node elem ) {
        Node kid;
        if( elem != null){
            if (elem.hasChildNodes()){
                for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                    if( kid.getNodeType() == Node.TEXT_NODE  ){
                        return kid.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    public ParseXMLFile(Segment[] segments, Graphics graphics) {
        this.segments = segments;
        this.g = graphics;
        this.rootControl = null;
        // parse XML file -> XML document will be build
        Document doc = parseFile(xmlFileName);
        // get root node of xml tree structure
        Node root = doc.getDocumentElement();
        // write node and its child nodes into System.out
        System.out.println("Statemend of XML document...");
        getInfo(root);
        System.out.println("... end of statement");
        // write Document into XML file
       // saveXMLDocument(targetFileName, doc);
    }
    
    private String getIndentSpaces(int indent) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < indent; i++) {
            buffer.append(" ");
        }
        return buffer.toString();
    }
           
    /** Writes node and all child nodes into System.out
     * @param node XML node from from XML tree wrom which will output statement start
     * @param indent number of spaces used to indent output
     */
    public void getInfo(Node node) {
        ParkedObject parkedObject= null;
        // get element name
        
        String nodeName = node.getNodeName();
        // get element value
        String nodeValue = getElementValue(node);
        // get attributes of element
        NamedNodeMap attributes = node.getAttributes();
        System.out.println(getIndentSpaces(index) + "NodeName: " + nodeName + ", NodeValue: " + nodeValue);
        //in the level 1
        if(nodeName.equals("street")|| nodeName.equals( "node")){
            index++;
            if(rootControl!=null)
                segments[index] = new Segment(rootControl);
            else
                segments[index] = new Segment();
                
        }else{
            //in the level 2
            if(nodeName.equals("parkedobject")){
                    parkedObject = new ParkedObject();
                    segments[index].addParkedObject(parkedObject);
            }
        }
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            //in the level 1
            if(nodeName.equals("street") || nodeName.equals("node"))
                    segments[index].setAttribute(attribute.getNodeName(),attribute.getNodeValue()) ;
            else
                //in the level 2
                if(nodeName.equals("parkedObject"))
                if(parkedObject != null)
                    parkedObject.setAttribute(attribute.getNodeName(),attribute.getNodeValue()) ;
                
            System.out.println(getIndentSpaces(index + 2) + "AttributeName: " + attribute.getNodeName() + ", attributeValue: " + attribute.getNodeValue());
        }
        // write all child nodes recursively
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                getInfo(child);
            }
        }
    }
    
    /** Saves XML Document into XML file.
     * @param fileName XML file name
     * @param doc XML document to save
     * @return <B>true</B> if method success <B>false</B> otherwise
     */    
    public boolean saveXMLDocument(String fileName, Document doc) {
        System.out.println("Saving XML file... " + fileName);
        // open output stream where XML Document will be saved
        File xmlOutputFile = new File(fileName);
        FileOutputStream fos;
        Transformer transformer;
        try {
            fos = new FileOutputStream(xmlOutputFile);
        }
        catch (FileNotFoundException e) {
            System.out.println("Error occured: " + e.getMessage());
            return false;
        }
        // Use a Transformer for output
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        try {
            transformer = transformerFactory.newTransformer();
        }
        catch (TransformerConfigurationException e) {
            System.out.println("Transformer configuration error: " + e.getMessage());
            return false;
        }
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(fos);
        // transform source into result will do save
        try {
            transformer.transform(source, result);
        }
        catch (TransformerException e) {
            System.out.println("Error transform: " + e.getMessage());
        }
        System.out.println("XML file saved.");
        return true;
    }
    
    /** Parses XML file and returns XML document.
     * @param fileName XML file to parse
     * @return XML document or <B>null</B> if error occured
     */
    public Document parseFile(String fileName) {
        System.out.println("Parsing XML file... " + fileName);
        DocumentBuilder docBuilder;
        Document doc = null;
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        }
        catch (ParserConfigurationException e) {
            System.out.println("Wrong parser configuration: " + e.getMessage());
            return null;
        }
        File sourceFile = new File(fileName);
        try {
            doc = docBuilder.parse(sourceFile);
        }
        catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        }
        catch (IOException e) {
            System.out.println("Could not read source file: " + e.getMessage());
        }
        System.out.println("XML file parsed");
        return doc;
    }
    
    /** Starts XML parsing example
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //new ParseXMLFile();
    }
    
}
